<?php
class AttrAction extends CommonAction {
	
	
	public function _initialize()
    {
        parent::_initialize();
        $this->suppierids = M('Supplier_user')->Field('supplier_id')->where(array('user_id' => session(C('USER_AUTH_KEY'))))->buildSql();
        $this->is_suppierids = M('Supplier_user')->Field('supplier_id')->where(array('user_id' => session(C('USER_AUTH_KEY'))))->count();

    }
	
	
	
	function index(){
		$this->listmap=1;
		$this->listmap.=" and a.site_id=".$this->siteid;
		if ($this->is_suppierids > 0) {
			$this->listmap.=" and (a.user_id=0 or a.user_id=".session(C('USER_AUTH_KEY')).')';
			//$_POST['user_id'] = session(C('USER_AUTH_KEY'));
		}else{
			$this->listmap.=" and a.user_id=0";
		}
		if($this->_get('type_id')){
			$this->listmap.=" and type_id=".$this->_get('type_id');
			$this->typename = M('Type') -> where(array('site_id' => $this->siteid, 'id' => $this->_get('type_id'))) -> getField('title');
			$this->type_id=$this->_get('type_id');
		}
		
        $this->_list('a',"left join __TYPE__ as t on a.type_id=t.id",'a.*,t.title as typename');
		$this->display();
	}
	
	//添加属性
	public function add(){
		$this->type_list=M("Type")->where(array('site_id'=>$this->siteid))->select();
		if($this->_get('type_id')){
			$this->type_id=$this->_get('type_id');
		}
		parent::add();
	}
	
	//编辑属性
	public function edit(){
		$this->type_list=M("Type")->where(array('site_id'=>$this->siteid))->select();
		parent::edit();
	}
	
	public function update(){
		//系统属性无法更新
		$attr = $this -> dao -> where(array('id' => $_POST['id'])) -> find();
		if(!session(C('ADMIN_AUTH_KEY')) && $this->is_suppierids > 0){
			if($attr['user_id'] != session(C('USER_AUTH_KEY'))){
				$this->error('您对当前的记录没有修改权');exit;
			}
		}
		parent::update(array(),U('index',array('type_id'=>$this->_post('type_id'))));
	}
	public function insert(){
		if ($this->is_suppierids > 0) {
			$_POST['user_id'] = session(C('USER_AUTH_KEY'));
		}
		parent::insert('',U('index',array('type_id'=>$this->_post('type_id'))));
	}
}

?>